#define _CRT_SECURE_NO_WARNINGS 1

#include"ObjectPool.h"
#include"ConcurrentAlloc.h"

struct TreeNode
{
	int _val;
	TreeNode* _left;
	TreeNode* _right;
	TreeNode()
		:_val(0)
		, _left(nullptr)
		, _right(nullptr)
	{}
};
void TestObjectPool()
{
	// 申请释放的轮次
	const size_t Rounds = 3;
	// 每轮申请释放多少次
	const size_t N = 10000;
	size_t begin1 = clock();
	std::vector<TreeNode*> v1;
	v1.reserve(N);
	for (size_t j = 0; j < Rounds; ++j)
	{
		for (int i = 0; i < N; ++i)
		{
			v1.push_back(new TreeNode);
		}
		for (int i = 0; i < N; ++i)
		{
			delete v1[i];
		}
		v1.clear();
	}
	size_t end1 = clock();
	ObjectPool<TreeNode> TNPool;
	size_t begin2 = clock();
	std::vector<TreeNode*> v2;
	v2.reserve(N);
	for (size_t j = 0; j < Rounds; ++j)
	{
		for (int i = 0; i < N; ++i)
		{
			v2.push_back(TNPool.New());
		}
		for (int i = 0; i < N; ++i)
		{
			TNPool.Delete(v2[i]);
		}
		v2.clear();
	}
	size_t end2 = clock();
	cout << "new cost time:" << end1 - begin1 << endl;
	cout << "object pool cost time:" << end2 - begin2 << endl;
}

//测试TLS无锁变量独立
void Alloc1()
{
	for (size_t i = 0; i < 5; i++)
	{
		void* ptr = ConcurrentAlloc(6);
	}
}

void Alloc2()
{
	for (size_t i = 0; i < 5; i++)
	{
		void* ptr = ConcurrentAlloc(7);
	}
}

void TestConCurrentAlloc1()
{
	void* p1 = ConcurrentAlloc(6);
	void* p2 = ConcurrentAlloc(6);
	void* p3 = ConcurrentAlloc(6);
	void* p4 = ConcurrentAlloc(6);
	void* p5 = ConcurrentAlloc(6);
	void* p6 = ConcurrentAlloc(6);
	cout << p1 << endl;
	cout << p2 << endl;
	cout << p3 << endl;
	cout << p4 << endl;
	cout << p5 << endl;
	cout << p6 << endl;

	ConcurrentFree(p1);
	ConcurrentFree(p2);
	ConcurrentFree(p3);
	ConcurrentFree(p4);
	ConcurrentFree(p5);
	ConcurrentFree(p6);
}

void TestConCurrentAlloc2()
{
	//8bytes桶，1页可以切1024个
	for (size_t i = 0; i < 1024; i++)
	{
		void* p1 = ConcurrentAlloc(6);
	}
	void* p2 = ConcurrentAlloc(8);
}

void TestThreadAlloc1()
{
	std::vector<void*>v;
	for (size_t i = 0; i < 7; i++)
	{
		void* ptr = ConcurrentAlloc(6);
		v.push_back(ptr);
	}
	for (auto e:v)
	{
		ConcurrentFree(e);
	}
}

void TestThreadAlloc2()
{
	std::vector<void*>v;
	for (size_t i = 0; i < 7; i++)
	{
		void* ptr = ConcurrentAlloc(20);
		v.push_back(ptr);
	}
	for (auto e : v)
	{
		ConcurrentFree(e);
	}
}

void TestConcurrentThread()
{
	std::thread t1(TestThreadAlloc1);
	std::thread t2(TestThreadAlloc2);

	t1.join();
	t2.join();
}

void TestBigAlloc()
{
	void* p1 = ConcurrentAlloc(257 * 1024);//257kb直接去PageCache申请
	ConcurrentFree(p1);

	void* p2 = ConcurrentAlloc(129 * 1024 * 8);//大于128页直接向堆申请
	ConcurrentFree(p2);
}


//int main()
//{
//	//TestObjectPool();
//	//std::thread t1(Alloc1);
//	//t1.join();
//
//	//std::thread t2(Alloc2);
//	//t2.join();
//
//	//TestConCurrentAlloc1();
//	//TestConCurrentAlloc2();
//
//	//TestThreadAlloc1();
//
//	//TestConcurrentThread();
//
//	//TestBigAlloc();
//
//	return 0;
//}